package com.ideal.frame.sys.controller;

import com.ideal.frame.common.anno.PermissionAnnotation;
import com.ideal.frame.common.dto.ResponseModel;
import com.ideal.frame.common.enumerate.SecurityPermission;
import com.ideal.frame.sys.entity.SysMenu;
import com.ideal.frame.sys.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/sys/menu")
@Api(tags = "系统菜单")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @ApiOperation(value = "获取所有菜单列表", httpMethod = "GET", produces = "application/json", response = ResponseModel.class)
    @PostMapping("list")
    @PermissionAnnotation(SecurityPermission.sys_menu_view)
    public ResponseModel getMenuTree() {
        return ResponseModel.success(sysMenuService.getMenuTree());
    }

    @ApiOperation(value = "根据id查询菜单信息", httpMethod = "GET", produces = "application/json", response = ResponseModel.class)
    @GetMapping("getById/{id}")
    @PermissionAnnotation(SecurityPermission.sys_menu_view)
    public ResponseModel getById(@PathVariable Long id) {
        return ResponseModel.success(sysMenuService.getById(id));
    }

    @ApiOperation(value = "菜单保存", httpMethod = "POST", produces = "application/json", response = ResponseModel.class)
    @PostMapping("save")
    @PermissionAnnotation(SecurityPermission.sys_menu_edit)
    public ResponseModel save(@RequestBody @Validated SysMenu params) {
        sysMenuService.save(params);
        return ResponseModel.success();
    }

    @ApiOperation(value = "根据用户id获取用户左侧菜单", httpMethod = "GET", produces = "application/json", response = ResponseModel.class)
    @GetMapping("getMenuTreeByUser/{userId}")
    public ResponseModel getMenuTreeByUser(@PathVariable Long userId) {
        return ResponseModel.success(sysMenuService.getMenuTreeByUser(userId));
    }

    @ApiOperation(value = "删除菜单", httpMethod = "POST", produces = "application/json", response = ResponseModel.class)
    @DeleteMapping("del")
    @PermissionAnnotation(SecurityPermission.sys_menu_del)
    public ResponseModel del(@RequestParam("ids") List<Long> ids) {
        sysMenuService.delete(ids);
        return ResponseModel.success("删除成功");
    }

}
